//
//  BankCardVC.swift
//  kxf
//
//  Created by Mac on 2024/3/9.
//

import Foundation
import UIKit
import MCToast

class BankCardVC : WHBaseViewVC {
    
    var bankType = "merc"//商户结算卡
    
    override func viewWillAppear(_ animated: Bool) {
        if self.bankType == "merc"{
            sednQueryBankMsgRequest()
        }else{
            sendSettleBankMSgRequest()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        initUI()
        
    }
    lazy var whiteView : UIView = {
        let vi = UIView()
        vi.backgroundColor = .white
        vi.isUserInteractionEnabled = true
        vi.layer.cornerRadius = kFitWidth(8)
        vi.clipsToBounds = true
        
        return vi
    }()
    lazy var titleVm : RegisterViewHeadTitleVM = {
        let vm = RegisterViewHeadTitleVM.init(frame: .zero)
        vm.titleLabel.text = "结算卡"
        
        return vm
    }()
    lazy var bankMsgVm : BankMsgVM = {
        let vm = BankMsgVM.init(frame: CGRect.init(x: 0, y: kFitWidth(47), width: 0, height: 0))
        vm.updateBlock = {()in
            if self.bankType == "merc"{
                let vc = BankCardUpdateVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }else{
                let vc = BankCardAgentUpdateVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }
        }
        return vm
    }()
    lazy var tipsImgView : UIImageView = {
        let img = UIImageView()
        img.setImgLocal(imgName: "icon_tips")
        
        return img
    }()
    lazy var tipsLabel : UILabel = {
        let lab = UILabel()
        lab.text = "只能绑定一张银行卡，如需更换结算卡点击更换银行卡"
        lab.textColor = .COLOR_TEXT_BLACK666
        lab.font = .systemFont(ofSize: 13, weight: .regular)
        lab.adjustsFontSizeToFitWidth = true
        
        return lab
    }()
}

extension BankCardVC{
    func initUI() {
        initNavi(titleStr: "银行卡管理")
        view.backgroundColor = .COLOR_LIGHT_GREY
        
        view.addSubview(whiteView)
        whiteView.addSubview(titleVm)
        whiteView.addSubview(bankMsgVm)
        
        view.addSubview(tipsImgView)
        view.addSubview(tipsLabel)
        
        setConstrait()
    }
    func setConstrait()  {
        whiteView.snp.makeConstraints { make in
            make.left.width.equalToSuperview()
            make.top.equalTo(getNavigationBarHeight()+kFitWidth(10))
            make.height.equalTo(kFitWidth(211))
        }
        tipsImgView.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(32))
            make.top.equalTo(whiteView.snp.bottom).offset(kFitWidth(17))
            make.width.height.equalTo(kFitWidth(18))
        }
        tipsLabel.snp.makeConstraints { make in
            make.left.equalTo(tipsImgView.snp.right).offset(kFitWidth(3))
            make.centerY.lessThanOrEqualTo(tipsImgView)
            make.right.equalTo(kFitWidth(-20))
        }
    }
}

extension BankCardVC{
    func sednQueryBankMsgRequest() {
        MCToast.mc_loading()
        WHNetworkUtil.shareManager().GET(urlString: URL_bank_query) { responseObject in
            DLLog(message: "\(responseObject)")
            
            if responseObject["code"]as? Int ?? -1 == 200{
                self.bankMsgVm.updateUI(dict: responseObject["data"]as? NSDictionary ?? [:])
            }else{
                self.presentAlertVcNoAction(title: "\(responseObject["message"]as? String ?? "")", viewController: self)
            }
        }
    }
    func sendSettleBankMSgRequest() {
        MCToast.mc_loading()
        WHNetworkUtil.shareManager().GET(urlString: URL_settleBank_Agent) { responseObject in
            DLLog(message: "\(responseObject)")
            if responseObject["code"]as? Int ?? 0 == 200{
                let dataArr = responseObject["data"]as? NSArray ?? []
                let dict = dataArr[0]as? NSDictionary ?? [:]
                self.bankMsgVm.updateUIForAgent(dict: dict)
            }else{
                self.presentAlertVcNoAction(title: "\(responseObject["message"]as? String ?? "网络异常，请稍后重试")", viewController: self)
            }
        }
    }
}
